tywx.TuyooSDK = {
    SESSION_KEY: "TU_SESSION_STORAGE",
    login: function() {
        tywx.TuyooSDK.getSystemInfo(), tywx.TuyooSDK.wechatLogin();
    },
    login2: function() {
        tywx.SystemInfo.wxAppId = "wx6ac3f5090a6b99c5", tywx.TuyooSDK.getSystemInfo(), tywx.TuyooSDK.guestLogin();
    },
    wechatLogin: function() {
        wx.login({
            success: function(t) {
                if (t.code) {
                    var e = t.code;
                    tywx.TuyooSDK.loginTuyooWithCode(e, {}), tywx.NotificationCenter.trigger(tywx.EventType.WEIXIN_LOGIN_SUCCESS);
                } else tywx.NotificationCenter.trigger(tywx.EventType.WEIXIN_LOGIN_FAIL);
            },
            fail: function(t) {
                tywx.NotificationCenter.trigger(tywx.EventType.WEIXIN_LOGIN_FAIL);
            },
            complete: function(t) {}
        });
    },
    loginTuyooWith3rdSession: function() {
        wx.getStorage({
            key: tywx.TuyooSDK.SESSION_KEY,
            success: function(t) {
                if (t.data) {
                    var e = tywx.SystemInfo.loginUrl + "open/v3/user/processSnsIdNew", o = {
                        snsId: t.data,
                        deviceName: "wechatGame",
                        clientId: tywx.SystemInfo.clientId,
                        appId: tywx.SystemInfo.appId
                    };
                    wx.request({
                        url: e,
                        data: o,
                        success: function(t) {},
                        fail: function(t) {},
                        complete: function(t) {}
                    });
                } else tywx.TuyooSDK.wechatLogin();
            },
            fail: function(t) {
                tywx.TuyooSDK.wechatLogin();
            },
            complete: function(t) {}
        });
    },
    normalLogin: function() {
        wx.login({
            success: function(t) {
                if (t.code) {
                    var e = t.code, o = function(t) {
                        wx.getUserInfo({
                            success: function(e) {
                                tywx.TuyooSDK.loginTuyooWithCode(t, e.userInfo);
                            },
                            fail: function(t) {}
                        });
                    };
                    wx.getSetting({
                        success: function(t) {
                            t.authSetting["scope.userInfo"] ? o(e) : wx.authorize({
                                scope: "scope.userInfo",
                                success: function() {
                                    o(e);
                                },
                                fail: function() {
                                    tywx.NotificationCenter.trigger(tywx.EventType.START_AUTHORIZATION_FAILED);
                                },
                                complete: function() {}
                            });
                        }
                    });
                }
            },
            fail: function(t) {},
            complete: function(t) {}
        });
    },
    guestLogin: function() {
        wx.login({
            success: function(t) {
                var e = {
                    nickName: "测试A",
                    avatarUrl: "url",
                    gender: 1
                };
                tywx.TuyooSDK.loginTuyooWithCode(t.code, e);
            },
            fail: function(t) {}
        });
    },
    loginTuyooWithCode: function(t, e) {
        var o = e.gender || 0, n = tywx.Util.getLocalUUID(), i = tywx.SystemInfo.loginUrl + "open/v6/user/LoginBySnsIdNoVerify", s = {
            "content-type": "application/x-www-form-urlencoded"
        }, c = {
            appId: tywx.SystemInfo.appId,
            wxAppId: tywx.SystemInfo.wxAppId,
            clientId: tywx.SystemInfo.clientId,
            snsId: "wxapp:" + t,
            uuid: n,
            gender: o
        };
        wx.request({
            url: i,
            header: s,
            data: c,
            method: "POST",
            success: function(t) {
                try {
                    var e = t.data;
                    if (e.error && 1 == e.error.code) return void console.log("tuyoo login fail...");
                    var o = e.result;
                    tywx.UserInfo.userId = o.userId, tywx.UserInfo.userName = o.userName, tywx.UserInfo.userPic = o.purl, 
                    tywx.UserInfo.authorCode = o.authorCode, tywx.UserInfo.wxgame_session_key = o.wxgame_session_key;
                    var n = o.token;
                    tywx.LOGD(null, "token:" + n), wx.setStorage({
                        key: tywx.TuyooSDK.SESSION_KEY,
                        data: n
                    }), tywx.TuyooSDK.initWebSocketUrl(o), tywx.NotificationCenter.trigger(tywx.EventType.SDK_LOGIN_SUCCESS);
                } catch (t) {
                    tywx.NotificationCenter.trigger(tywx.EventType.SDK_LOGIN_FAIL);
                }
            },
            fail: function(t) {
                tywx.NotificationCenter.trigger(tywx.EventType.SDK_LOGIN_FAIL);
            },
            complete: function(t) {}
        });
    },
    initWebSocketUrl: function(t) {
        if (t && t.tcpsrv) {
            var e, o = t.tcpsrv.ip, n = t.tcpsrv.wsport || t.tcpsrv.port;
            e = tywx.SystemInfo.loginUrl.indexOf("170") > -1 || tywx.SystemInfo.loginUrl.indexOf("https://") > -1 ? "wss://" + o + "/" : "ws://" + o + ":" + n.toString() + "/", 
            tywx.SystemInfo.webSocketUrl = e;
        }
    },
    getSystemInfo: function() {
        wx.getSystemInfo({
            success: function(t) {
                var e = t.model.indexOf("iPhone") >= 0, o = t.windowHeight, n = 0;
                n = e ? 812 == o ? 2 : 736 == o ? 4 : 1 : 3, tywx.UserInfo.systemType = n, tywx.UserInfo.wechatType = t.version, 
                tywx.UserInfo.model = t.model, tywx.UserInfo.system = t.system;
            },
            fail: function() {},
            complete: function() {}
        });
    }
}, tywx.initTuyooSDK = function() {
    wx.onError(function(t) {
        var e = new Date(), o = e.toDateString() + " " + e.toTimeString() + "  " + t.message;
        tywx.BiLog.uploadLogTimely(o);
    });
};